<?php


namespace app\home\controller;


use app\base\BaseController;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Exception;
use think\facade\Db;
use think\facade\View;

/**
 * Class QuotationOrder
 * @package app\home\controller
 * 报价
 */
class QuotationOrder extends BaseController
{
    protected $title='报价';
    protected $table='quotation_order';

    public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['qo.status','>',-1];

            if (isset($this->param['status'])&&$this->param['status']){
                $where[]=['qo.status','=',$this->param['status']];
            }
            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['py.name','like','%'.$this->param['name'].'%'];
            }
            if (isset($this->param['orderNum'])&&$this->param['orderNum']){
                $where[]=['qo.orderNum','like','%'.$this->param['orderNum'].'%'];
            }
            if (isset($this->param['saleUnit'])&&$this->param['saleUnit']){
                $where[]=['d.title','like','%'.$this->param['saleUnit'].'%'];
            }

            if (isset($this->param['orderDate'])&&$this->param['orderDate']){
                $dates=explode('~',$this->param['orderDate']);
                $where[]=['qo.orderDate','between',$dates];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('qo')
                ->leftJoin('purveyor py','py.id=qo.punterId')
                ->leftJoin('department d','d.id=qo.salesUnitId')
//                ->leftJoin('admin ad','ad.id=qo.saleId')
                ->where($where)
                ->field('qo.*,py.name as pName,d.title as dName')
                ->order('qo.id DESC')
                ->paginate($rows)->each(function ($item){
                    $saleUsers=Db::name('admin')->where('id','in',$item['saleId'])->column('name');
                    $item['saleName']=implode(',',$saleUsers);
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
}

    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $param=$this->param;
            $mx=$param['mx']??[];
            unset($param['mx']);
            if (!$mx){
                return to_assign(1,'请添加明细');
            }
            if (!$param['punterId']||!$param['salesUnitId']
                ||!$param['saleId']){
                return to_assign(1,'参数有误');
            }
            $mxId=$mx['mxid']??[];
            $mdId=$mx['mdId']??[];
            $bjNum=$mx['bjNum']??[];
            $bjPrice=$mx['bjPrice']??[];
            $mdPrice=$mx['mdPrice']??[];
            $discount=$mx['discount']??[];
            $noTaxPrice=$mx['noTaxPrice']??[];
            $bjMoney=$mx['bjMoney']??[];
            $bjTime=$mx['bjTime']??[];
            $remark=$mx['remark']??[];
            $timeLong=$mx['timeLong']??[];
            $param['update_time']=time();
            $countMoney=0;
            foreach ($bjMoney as $v){
                $countMoney+=$v;
            }
            $param['countMoney']=$countMoney;
            $id=$this->param['id']??0;
            try {
                $param['orderNum']=dJBHBySql($this->table,'orderNum',getOrderPrefix($this->controller).date('Ymd'),false,1000);

                if ($id > 0) {
                    Db::name($this->table)->strict(false)->field(true)->update($param);
                    add_log('edit', $param['id'], $param,$this->title);
                } else {
                    $param['createId']=$this->uid;
                    $param['departId']=$this->did;
                    $param['create_time'] = time();
                    $id = Db::name($this->table)->strict(false)->field(true)->insertGetId($param);
                    add_log('add', $id, $this->param,$this->title);
                }
                foreach ($mdId as $key=> $m){
                    $mxData=[
                        'orderId'=>$id,
                        'mdId'=>$mdId[$key]??0,
                        'bjNum'=>$bjNum[$key]??0,
                        'bjPrice'=>$bjPrice[$key]??0,
                        'mdPrice'=>$mdPrice[$key]??0,
                        'discount'=>$discount[$key]??0,
                        'noTaxPrice'=>$noTaxPrice[$key]??0,
                        'bjMoney'=>$bjMoney[$key]??0,
                        'bjTime'=>$bjTime[$key]??0,
                        'remark'=>$remark[$key]??0,
                        'timeLong'=>$timeLong[$key]??0,
                        'create_time'=>time(),
                        'update_time'=>time()
                    ];
                    if (isset($mxId[$key])&&$mxId[$key]){
                        unset($mxData['create_time']);
                        $mxData['id']=$mxId[$key];
                    }
                    Db::name('quotation_order_detail')->save($mxData);
                }
            }catch (\Exception $e){
                return to_assign(1,'操作失败'.$e->getMessage());
            }

            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $detail=[];
            $mxList=[];
            $saleUserList=Db::name('admin')
//                    ->where('id','in',$detail['saleId'])
                ->where('status',1)
                ->field('id,name')->select();
            $countNum=0;
            $countPrice=0;
            if ($id > 0) {
                $detail = Db::name($this->table)->where('id', $id)->find();
                $mxList=Db::name('quotation_order_detail')->alias('qod')
                    ->leftJoin('material_dossier md','md.id=qod.mdId')
                    ->where('qod.status','>',-1)
                    ->where('qod.orderId',$id)
                    ->field('qod.*,md.name as mdName,md.buyCode,md.brand,md.specs,md.unit,md.bar_code')
                    ->select();
                $countNum=Db::name('quotation_order_detail')
                    ->where('status','>',-1)
                    ->where('orderId',$id)->sum('bjNum');
                $countPrice=Db::name('quotation_order_detail')
                    ->where('status','>',-1)
                    ->where('orderId',$id)->sum('bjMoney');

            }else{
                $detail['orderNum']=dJBHBySql($this->table,'orderNum',getOrderPrefix($this->controller).date('Ymd'),false,1000);
            }
            $punterList=Db::name('purveyor')->where('status',1)
                ->where('dw_type',2)
                ->field('id,name')->select();
            view::assign('punterList',$punterList);
            $saleUniList=Db::name('department')->where('status',1)->where('pid',0)->field('id,title')->select();
            view::assign('saleUniList',$saleUniList);
            View::assign('detail', $detail);
            View::assign('mxList', $mxList);
            View::assign('id', $id);
            View::assign('saleUserList', $saleUserList);
            $thisDate=date('Y-m-d');
            View::assign('thisDate',$thisDate);
            $payTypeList=Db::name('quo_order_pay_type')
                ->where('status',1)
                ->field('id,name')->select();
            View::assign('payTypeList',$payTypeList);
            $quoOrderTax=getOrderTax();
            View::assign('quoOrderTax',$quoOrderTax);
            View::assign(['countPrice'=>$countPrice,'countNum'=>$countNum]);
            return view('form');
        }
    }

    public function info($id='')
    {
        $detail = Db::name($this->table)->where('id', $id)->find();
        $mxList=Db::name('quotation_order_detail')->alias('qod')
            ->leftJoin('material_dossier md','md.id=qod.mdId')
            ->where('qod.status','>',-1)
            ->where('qod.orderId',$id)
            ->field('qod.*,md.name as mdName,md.buyCode,md.brand,md.specs,md.unit,md.bar_code')
            ->select();
        $punterList=Db::name('purveyor')->where('status',1)
            ->where('dw_type',2)
            ->field('id,name')->select();
        view::assign('punterList',$punterList);
        $saleUniList=Db::name('department')->where('status',1)->where('pid',0)->field('id,title')->select();
        view::assign('saleUniList',$saleUniList);
        View::assign('detail', $detail);
        View::assign('mxList', $mxList);
        $saleUserList=Db::name('admin')
//                    ->where('id','in',$detail['saleId'])
            ->where('status',1)
            ->field('id,name')->select();
        View::assign('saleUserList', $saleUserList);
        View::assign('id', $id);
        $payTypeList=Db::name('quo_order_pay_type')
            ->where('status',1)
            ->field('id,name')->select();
        View::assign('payTypeList',$payTypeList);
        $quoOrderTax=getOrderTax();
        View::assign('quoOrderTax',$quoOrderTax);
        $countNum=Db::name('quotation_order_detail')
            ->where('status','>',-1)
            ->where('orderId',$id)->sum('bjNum');
        $countPrice=Db::name('quotation_order_detail')
            ->where('status','>',-1)
            ->where('orderId',$id)->sum('bjMoney');
        View::assign(['countPrice'=>$countPrice,'countNum'=>$countNum]);
        return view();

}
    public function onSure()
    {
        $id=$this->param['id']??0;
        if (!$id){
            return to_assign(1,'参数缺失');
        }
        $mx=Db::name('quotation_order_detail')->where('status',1)->where('orderId',$id)->count();
        if (!$mx){
            return to_assign(1,'请添加明细');
        }

        Db::startTrans();
        try {
            Db::name($this->table)->where('id',$id)->update([
                'sureId'=>$this->uid,
                'status'=>2,
                'update_time'=>time(),'checkTime'=>time()]);
            add_log('onSure', $id, $this->param,$this->title);
            Db::name('quotation_order_detail')->where('status',1)
                ->where('orderId',$id)->update(['status'=>2,'update_time'=>time()]);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign(0,'操作成功');
    }
    public function addMx()
    {
       $punterId=$this->param['punterId']??'';
        if (request()->isPost()) {
            $where[]=['status','=',1];
            foreach (['bar_code','name','brand','specs','buyCode'] as $v){
                if (isset($this->param[$v])&&$this->param[$v]){
                    $where[]=[$v,'like','%'.$this->param[$v].'%'];
                }
            }
            $limit=$this->param['limit']??15;
            $list=Db::name('material_dossier')
                ->field("*")
                ->where($where)->paginate($limit)->each(function ($item)use ($punterId){
                    $hsLocal=[];
                    if ($punterId){
                        $hsLocal=Db::name('quotation_order_detail')
                            ->alias('qod')
                            ->leftJoin('quotation_order qo','qo.id=qod.orderId')
                            ->where('qo.punterId',$punterId)
                            ->where('qod.mdId',$item['id'])
                            ->where('qod.status','>',0)
                            ->order('qod.id DESC')
                            ->field('qod.*')
                            ->find();
                        
                    }
                    $item['bjNum']=$hsLocal['bjNum']??'';
                    $item['bjPrice']=$hsLocal['bjPrice']??'';
                    $item['mdPrice']=$hsLocal['mdPrice']??'';
                    $item['discount']=$hsLocal['discount']??'';
                    $item['noTaxPrice']=$hsLocal['noTaxPrice']??'';
                    $item['bjMoney']=$hsLocal['bjMoney']??'';
                    $item['bjTime']=$hsLocal['bjTime']??'';
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            View::assign('punterId',$punterId);
            return view('mx');
        }
    }

    public function removeMx()
    {
        $ids=$this->param['id']??[];
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
        Db::startTrans();
        try {
            Db::name('quotation_order_detail')
                ->where('id',$ids)->update(['status'=>-1,'deleted'=>time()]);
            add_log('delete', $ids, $this->param,$this->title.'明细');
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }

        return to_assign();
    }

    public function delete()
    {
        $ids=$this->param['id']??[];
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
        $statusArr=Db::name($this->table)->where('id','in',$ids)->column('status');
        if (in_array('2',$statusArr)){
            return to_assign(1,'已确认不能删除');
        }
        Db::startTrans();
        try {
            Db::name($this->table)->where('id','in',$ids)->update(['status'=>-1,'deleted'=>time()]);
            Db::name('quotation_order_detail')
                ->where('status','>',-1)
                ->where('orderId','in',$ids)->update(['status'=>-1,'deleted'=>time()]);
            add_log('delete', $ids, $this->param,$this->title);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }

        return to_assign();
    }

    public function importData()
    {
        Db::startTrans();
        try {
            if (!isset($_FILES['file'])){
                throw new Exception('请选择上传文件');
            }
            //限制上传表格类型
            $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
            //application/vnd.ms-excel  为xls文件类型
            if ($fileExtendName != 'xlsx') {
                throw new Exception('必须为excel表格，且必须为xlsx格式！');
            }

            if (is_uploaded_file($_FILES['file']['tmp_name'])) {
                // 有Xls和Xlsx格式两种
//            $objReader = \IOFactory::createReader('Xlsx');
                $objReader = IOFactory::createReader('Xlsx');

                $filename = $_FILES['file']['tmp_name'];

                $objPHPExcel = $objReader->setReadDataOnly(true)->load($filename);  //$filename可以是上传的表格，或者是指定的表格

                $sheet = $objPHPExcel->getSheet(0);   //excel中的第一张sheet
                $highestRow = $sheet->getHighestRow();       // 取得总行数
                $importData = [];
                $punterId=[];
                $salesUnitId=[];
                $saleId=[];
                $tax=[];
                $contactId=[];
                $saveId=[];
                $orderDate=[];
                $mdId=[];
                $bjNum=[];
                $bjPrice=[];
                $discount=[];
                $bjTime=[];
                $remark=[];
                $timeLong=[];
                //循环读取excel表格，整合成数组。如果是不指定key的二维，就用$data[i][j]表示。
                for ($j = 2; $j <= $highestRow; $j++) {
                    $punterId[]=(int)$objPHPExcel->getActiveSheet()->getCell("A" . $j)->getFormattedValue();
                    if ($punterId){
                    $salesUnitId[]=(int)$objPHPExcel->getActiveSheet()->getCell("B" . $j)->getFormattedValue();
                    $saleId[]=(int)$objPHPExcel->getActiveSheet()->getCell("C" . $j)->getFormattedValue();
                    $tax[]=(float)$objPHPExcel->getActiveSheet()->getCell("D" . $j)->getFormattedValue();
                    $contactId[]=(int)$objPHPExcel->getActiveSheet()->getCell("E" . $j)->getFormattedValue();
                    $saveId[]=(int)$objPHPExcel->getActiveSheet()->getCell("F" . $j)->getFormattedValue();
                    $orderDate[]=(string)$objPHPExcel->getActiveSheet()->getCell("G" . $j)->getFormattedValue();
                    $mdId[]=(int)$objPHPExcel->getActiveSheet()->getCell("H" . $j)->getFormattedValue();
                    $bjNum[]=(float)$objPHPExcel->getActiveSheet()->getCell("I" . $j)->getFormattedValue();
                    $bjPrice[]=(float)$objPHPExcel->getActiveSheet()->getCell("J" . $j)->getFormattedValue();
                    $discount[]=(float)$objPHPExcel->getActiveSheet()->getCell("K" . $j)->getFormattedValue();
                    $bjTime[]=$objPHPExcel->getActiveSheet()->getCell("L" . $j)->getFormattedValue();
                    $remark[]=$objPHPExcel->getActiveSheet()->getCell("M" . $j)->getFormattedValue();
                    $timeLong[]=(int)$objPHPExcel->getActiveSheet()->getCell("N" . $j)->getFormattedValue();
                    }
                }
                $res=$this->createImportData($punterId,$salesUnitId,$saleId,$tax,
                    $contactId,$saveId,$orderDate,$mdId,$bjNum,$bjPrice,$discount,$bjTime,$remark,$timeLong);
                if ($res['code']!=0){
                    throw new Exception($res['msg']);
                }
                $zhuData=$res['data']['orderData'];
                $mxData=$res['data']['mxData'];
                if ($zhuData){
                   $id= Db::name($this->table)->insertGetId($zhuData);
                   foreach ($mxData as &$v){
                       $v['orderId']=$id;
                   }
                   Db::name('quotation_order_detail')->insertAll($mxData);
                    add_log('import', $id,$res['data'] ,$this->title);
                }
                Db::commit();
            }

        }catch (Exception $e){
            Db::rollback();
            add_log('import', '', $e->getMessage(),$this->title);
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign();

    }

    public function createImportData($punterId,$salesUnitId,$saleId,$tax,$contactId,$saveId,$orderDate,$mdId,$bjNum,$bjPrice,$discount,$bjTime,$remark,$timeLong)
    {
        $punterId=array_unique($punterId);
        $salesUnitId=array_unique($salesUnitId);
        $saleId=array_unique($saleId);
        $tax=array_unique($tax);
        $contactId=array_unique($contactId);
        $saveId=array_unique($saveId);
        $orderDate=array_unique($orderDate);
        if (count($punterId)!=1||count($salesUnitId)!=1||count($saleId)!=1||count($tax)!=1
            ||count($contactId)!=1||count($saveId)!=1||count($orderDate)!=1){
            return ['code'=>1,'msg'=>'参数不一致'];
        }
        $punterId1=Db::name('purveyor')->where('status',1)->where('id',$punterId[0])->value('id');
        $salesUnitId1=Db::name('department')->where('status',1)->where('id',$salesUnitId[0])->value('id');
        $saleId1=Db::name('admin')->where('did',$salesUnitId1)->where('status',1)->where('id',$saleId[0])->value('id');
        $contactId1=Db::name('purveyor_member')->where('purveyorId',$punterId1)
            ->where('status',1)->where('type',1)->where('id',$contactId[0])->find();
        $saveId1=Db::name('purveyor_member')->where('purveyorId',$punterId1)
            ->where('status',1)->where('type',2)->where('id',$saveId[0])->find();
        if (!$punterId1||!$salesUnitId1||!$saleId1||!$contactId1||!$saveId1){
            return  ['code'=>1,'msg'=>'参数有误'];
        }
        $zuData=[
            'orderNum'=>dJBHBySql($this->table,'orderNum',getOrderPrefix($this->controller).date('Ymd'),false,1000),
            'punterId'=>$punterId1,
            'salesUnitId'=>$salesUnitId1,
            'saleId'=>$saleId1,
            'contactId'=>$contactId1['id'],
            'contactName'=>$contactId1['name'],
            'contactPhone'=>$contactId1['phone'],
            'saveId'=>$saveId1['id'],
            'saveName'=>$saveId1['name'],
            'savePhone'=>$saveId1['phone'],
            'saveAdd'=>$saveId1['address'],
            'orderDate'=>$orderDate[0],
            'tax'=>$tax[0],
            'createId'=>$this->uid,
            'departId'=>$this->did,
            'create_time'=>time(),
            'update_time'=>time()
        ];
        $countMoney=0;
        $mxData=[];
        $tax1=$tax[0]/100;
        foreach ($mdId as $k=>$v){
            if (empty($mdId[$k])||empty($bjNum[$k])||empty($discount[$k])||empty($bjTime[$k])||empty($timeLong[$k])){
                return ['code'=>1,'msg'=>$k.'行参数不能为空'];
            }
            $mdInfo=Db::name('material_dossier')->where('status',1)->where('id',$v)->find();
            if (!$mdInfo){
                return ['code'=>1,'msg'=>$k.'行物料不存在'];
            }

            $discount1=$discount[$k]/100;
            $bjNum1=$bjNum[$k];
            $bjPrice1=$bjPrice[$k];
            $noTaxPrice=round((($bjPrice1*$discount1)/(1-$tax1)),2);
            $bjMoney=round(($bjPrice1*$discount1*$bjNum1),2);
            $countMoney+=$bjMoney;
            $mxData[]=[
                'mdId'=>$mdId[$k],
                'bjNum'=>$bjNum[$k],
                'bjPrice'=>$bjPrice[$k],
                'mdPrice'=>$mdInfo['proPrice'],
                'discount'=>$discount[$k],
                'noTaxPrice'=>$noTaxPrice,
                'bjMoney'=>$bjMoney,
                'bjTime'=>$bjTime[$k],
                'remark'=>$remark[$k]??'',
                'timeLong'=>$timeLong[$k],
                'create_time'=>time(),
                'update_time'=>time(),
            ];
        }
        $zuData['countMoney']=$countMoney;
        return ['code'=>0,'msg'=>'操作成功','data'=>['orderData'=>$zuData,'mxData'=>$mxData]];
    }

    public function toHetong()
    {
        $ids=$this->param['ids']??'';
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
      Db::startTrans();
        try {
            $status=Db::name($this->table)->where('id','in',$ids)->column('status');
            if (in_array('1',$status)){
                throw new Exception('请选择已确认数据');
            }
            $list=Db::name($this->table)->where('id','in',$ids)->select();
            foreach ($list as $v){
                $contractData=[
                    'orderNum'=>dJBHBySql('sale_contract','orderNum',getOrderPrefix('SaleContract').date('Ymd'),false,1000),
                    'punterId'=>$v['punterId'],
                    'salesUnitId'=>$v['salesUnitId'],
                    'saleId'=>$v['saleId'],
                    'tax'=>$v['tax'],
                    'type'=>1,
                    'quotationOrderId'=>$v['id'],
                    'countMoney'=>$v['countMoney'],
                    'contactId'=>$v['contactId'],
                    'contactName'=>$v['contactName'],
                    'contactPhone'=>$v['contactPhone'],
                    'saveId'=>$v['saveId'],
                    'saveName'=>$v['saveName'],
                    'savePhone'=>$v['savePhone'],
                    'saveAdd'=>$v['saveAdd'],
                    'countDiscount'=>0,
                    'orderDate'=>date('Y-m-d'),
                    'createId'=>$this->uid,
                    'departId'=>$this->did,
                    'create_time'=>time(),
                    'update_time'=>time(),

                ];
                $mxList=Db::name('quotation_order_detail')->where('orderId',$v['id'])->select();
                $orderId=Db::name('sale_contract')->insertGetId($contractData);
                $contractMx=[];
                foreach ($mxList as $ml){
                    $contractMx[]=[
                        'orderId'=>$orderId,
                        'mdId'=>$ml['mdId'],
                        'type'=>1,
                        'quotationOderNum'=>$v['orderNum'],
                        'quotationDetailId'=>$ml['id'],
                        'bjNum'=>$ml['bjNum'],
                        'bjPrice'=>$ml['bjPrice'],
                        'noTaxPrice'=>$ml['noTaxPrice'],
                        'noTaxMoney'=>round($ml['noTaxPrice']*$ml['bjNum'],2),
                        'taxMoney'=>abs($ml['bjNum']-round($ml['noTaxPrice']*$ml['bjNum'],2)),
                        'bjMoney'=>$ml['bjMoney'],
                        'proCode'=>'',
                        'disAmount'=>0,
                        'khPrice'=>0,
                        'bjTime'=>$ml['bjTime'],
                        'create_time'=>time(),
                        'update_time'=>time(),
                    ];

                }
                Db::name('sale_contract_detail')->insertAll($contractMx);
            }
            add_log('toHetong', $ids, $this->param,$this->title.'转合同');
            Db::commit();
        } catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败：'.$e->getMessage());
        }

        return to_assign();
    }

    public function importMxData()
    {
//        Db::startTrans();
        try {
            if (!isset($_FILES['file'])){
                throw new Exception('请选择上传文件');
            }
            //限制上传表格类型
            $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
            //application/vnd.ms-excel  为xls文件类型
            if ($fileExtendName != 'xlsx') {
                throw new Exception('必须为excel表格，且必须为xlsx格式！');
            }

            if (is_uploaded_file($_FILES['file']['tmp_name'])) {
                // 有Xls和Xlsx格式两种
//            $objReader = \IOFactory::createReader('Xlsx');
                $objReader = IOFactory::createReader('Xlsx');

                $filename = $_FILES['file']['tmp_name'];

                $objPHPExcel = $objReader->setReadDataOnly(true)->load($filename);  //$filename可以是上传的表格，或者是指定的表格

                $sheet = $objPHPExcel->getSheet(0);   //excel中的第一张sheet
                $highestRow = $sheet->getHighestRow();       // 取得总行数
                $importData = [];
                $specData=[];
                //循环读取excel表格，整合成数组。如果是不指定key的二维，就用$data[i][j]表示。
                for ($j = 2; $j <= $highestRow; $j++) {
                    $specs=$objPHPExcel->getActiveSheet()->getCell("A" . $j)->getFormattedValue();
                    $mdInfo=Db::name('material_dossier')->where('status',1)
                        ->where('specs',$specs)->find();
                    if (!$mdInfo){
                        $warText="第".($j-1) ."行物料不存在";
                        throw new Exception($warText);
                    }
                    $importData[]=$mdInfo;
                }
//                Db::commit();
            }

        }catch (Exception $e){
//            Db::rollback();
            add_log('import', '', $e->getMessage(),$this->title);
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign(0,'操作成功',$importData);
    }

    public function onCancel()
    {
        $id=$this->param['id']??'';
        $info=Db::name($this->table)->where('id',$id)->find();
        if (!$info||$info['status']!=2){
            return to_assign(1,'操作有误');
        }
        Db::startTrans();
        try {
            Db::name($this->table)->where('id',$id)->update([
                'sureId'=>$this->uid,
                'status'=>1,
                'update_time'=>time(),'checkTime'=>time()]);
            add_log('back', $id, $this->param,$this->title);
            Db::name('quotation_order_detail')->where('status',2)
                ->where('orderId',$id)->update(['status'=>1,'update_time'=>time()]);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign();
    }
}